﻿using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using KPIS.GERP.GBMS.APP_UI.Common;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GBMS.BLL;
using KPIS.GERP.GBMS.MODEL;

namespace KPIS.GERP.GBMS.APP_UI.Form
{
    public partial class ExportReport : System.Web.UI.Page
    {
        MailMerge mailMerge = new MailMerge();
        Main main = new Main();
        protected void Page_Load(object sender, EventArgs e)
        {
            string strReportName = Request.QueryString["report"];
            LoadReport(strReportName);
        }

        private void LoadReport(string strReportName)
        {
            switch (strReportName)
            {
                case "ExpenseControlSummary":
                    LoadExpenseControlSummary();
                    break;
            }
        }

        private void LoadExpenseControlSummary()
        {
            ProjectControl dal = new ProjectControl();
            ReportDocument rpt = new ReportDocument();
            rpt.Load(Server.MapPath("~/Reports/ExpenseControlSummary.rpt"));

            int nBgmYear = SystemInfo._CurrentYear;

            var dtb1 = dal.GetProjectExpenseControlSummary(nBgmYear, "1");
            var dtb2 = dal.GetProjectExpenseControlSummary(nBgmYear, "2");
            var dtb3 = dal.GetProjectExpenseControlSummary(nBgmYear, "3");

            rpt.OpenSubreport("ExpenseControlSummarySub1.rpt");
            rpt.OpenSubreport("ExpenseControlSummarySub2.rpt");
            rpt.OpenSubreport("ExpenseControlSummarySub3.rpt");
            rpt.Subreports[0].SetDataSource(dtb1);
            rpt.Subreports[1].SetDataSource(dtb2);
            rpt.Subreports[2].SetDataSource(dtb3);

            rpt.ExportToHttpResponse(ExportFormatType.Excel, Response, true, HttpUtility.UrlEncode("สรุปเงินกองกลางคงเหลือ", System.Text.Encoding.UTF8));
        }
    }
}